<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-calendar"></i> 药品采购
                </el-breadcrumb-item>
                <el-breadcrumb-item>新增供应商</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="form-box">
                <el-form ref="formRef" :rules="rules" :model="form" label-width="80px">
                    <el-form-item label="供应商" prop="supplierName">
                        <el-input v-model="form.supplierName"></el-input>
                    </el-form-item>
                    <el-form-item label="负责人" prop="supplierPrincipal">
                        <el-input v-model="form.supplierPrincipal"></el-input>
                    </el-form-item>
                    <el-form-item label="地址" prop="supplierAddress">
                        <el-input v-model="form.supplierAddress"></el-input>
                    </el-form-item>
                    <el-form-item label="联系方式" prop="supplierPhone">
                        <el-input v-model="form.supplierPhone"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="onSubmit">新增供应商</el-button>
                        <el-button @click="onReset">清空表单</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </div>
    </div>
</template>

<script>
import { reactive, ref } from "vue";
import { ElMessage } from "element-plus";
import { addSupplier } from "../../api";
export default {
    name: "supplierForm",
    setup() {
        const rules = {
            supplierName: [
                { required: true, message: "请输入供应商名称", trigger: "blur" },
            ],
            supplierPrincipal: [
                { required: true, message: "请输入负责人姓名", trigger: "blur" },
            ],
            supplierPhone: [
                { required: true, message: "请输入联系方式", trigger: "blur" },
            ],
            supplierAddress: [
                { required: true, message: "请输入供应商地址", trigger: "blur" },
            ]
        };
        const formRef = ref(null);

        // 表单数据
        let form = reactive({
            supplierName: '',
            supplierPrincipal: '',
            supplierPhone: '',
            supplierAddress: ''
        });

        // 重置
        const onReset = () => {
            formRef.value.resetFields();
        };

        // 提交
        const onSubmit = () => {
            // 表单校验
            formRef.value.validate((valid) => {
                if (valid) {
                    console.log(form);
                    addSupplier(form).then(res => {
                        if (res.code === 200) {
                            // reload();
                            onReset();
                            ElMessage.success('添加成功');
                        } else {
                            console.log("[添加供应商] 失败: " + res.message);
                            ElMessage.error('服务器繁忙，请稍后重试');
                        }
                    })
                } else {
                    return false;
                }
            });
        };

        return {
            rules,
            formRef,
            form,
            onSubmit,
            onReset,
        };
    },
};
</script>